<template>
  <section>
    <el-card class="search-card">
      <BasicTableForm ref="formData"
        isScalingBtn
        formItemWidth="120px"
        :colNumOfRow="4"
        :formColumns="formItemList"
        :dataSource="formData"
        :defaultExpandStatus="false"
        :handleSearchForm="handleQueryForm" />
    </el-card>
    <el-card class="table-card">
      <basic-table
        :showSelectCol="false"
        :loading="tableInfo.loading"
        :columns="tableInfo.columns"
        :dataSource="tableInfo.list"
        :pagination="pagination"
        :tableHeight="460"
        @pageChange="handlePageChange"
        @pageSizeChange="handlePageSizeChange"
      >
        <div slot="content_context">
          <el-button
            v-if="showBtn('导出') && supplierExportConfig.supervision"
            :disabled="!pagination.total"
            size="mini"
            type="primary"
            @click="handleExport"
            >导出</el-button>
        </div>
      </basic-table>
    </el-card>
    <!-- 导出 -->
    <ExportDialog
      :visible.sync="visible"
      :exportInfo='exportInfo'
      :total='pagination.total'
    />
  </section>
</template>

<script>
import { mapGetters } from 'vuex';
import BasicTableForm from '@/components/supplier/BasicForm'; // 表单组件
import BasicTable from '@/components/supplier/BasicTable';
import ExportDialog from '@supplier/components/ExportDialog';
import {
  querySupervisionSurveillanceListService,
  exportSupervisionSurveillanceListService
} from '@/api/supplier/supervision/surveillance';
import { getHasIndexTableData } from '@/pages/supplier/utils/bizUtils';
import { replaceNullItems } from '@/pages/supplier/utils';
import { INITIAL_FIELDS_VALUE, FORM_ITEM_LIST, TABLE_COLUMNS } from './constants';

export default {
  components: {
    BasicTableForm,
    BasicTable,
    ExportDialog
  },
  computed: {
    ...mapGetters('permissions/systemBtns', ['showBtn']),
  },
  data () {
    return {
      formItemList: FORM_ITEM_LIST,
      tableInfo: {
        list: [],
        selection: [],
        columns: TABLE_COLUMNS,
        loading: false
      },
      pagination: {
        current: 1,
        total: 0,
        size: 10
      },
      formData: Object.assign({}, INITIAL_FIELDS_VALUE),
      visible: false,
      exportInfo: {}
    };
  },
  methods: {
    handleQueryForm () {
      this.getTableData(1);
    },
    // 页码切换
    handlePageChange (val) {
      this.getTableData(val);
    },
    // 每页条数切换
    handlePageSizeChange (val) {
      this.getTableData(1, val);
    },
    // 获取列表查询参数
    getRequestParams () {
      const {
        supplierCode,
        supplierName,
        productCode,
        productName,
        managementArea,
        belongDepartment,
        saleAreaUuid,
      } = this.formData
      return {
        supplierCode,
        supplierName,
        productCode,
        productName,
        managementArea,
        belongDepartment,
        saleAreaUuid,
      }
    },
    async getTableData (current = this.pagination.current, size = this.pagination.size) {
      const params = Object.assign({ current, size }, this.getRequestParams())
      const resp = await querySupervisionSurveillanceListService(params, state => { this.tableInfo.loading = state })
      if (resp && resp.result) {
        const { records, total, pages } = resp.result
        this.pagination = Object.assign(this.pagination, { current, size, total, pages })
        this.tableInfo.list = getHasIndexTableData(replaceNullItems(records), this.pagination)
      } else {
        this.tableInfo.list = []
      }
    },
    // 导出
    handleExport () {
      this.visible = true;
      this.exportInfo = {
        service: exportSupervisionSurveillanceListService,
        excelTitle: '监察核查信息',
        params: this.getRequestParams(),
      }
    },
  }
}
</script>
